import Api from 'api/tactics'

let state = {
  sport_list: [], // 运动类型列表
  sport: {},
  tactics_list: [], // 战术板列表
  tactics_type_list: [], // 战术板类型列表
  tactics_info: {
    key: '',
    id: ''
  } // 战术板信息
}
const getters = {
  tactics_list: state => state.tactics_list,
  tactics_type_list: state => state.tactics_type_list,
  sport: state => state.sport,
  tactics_info: state => state.tactics_info
}

const actions = {
  tactics_list ({ commit, state }, params) {
    return Api.typeNameList(params).then((json) => {
      commit('TACTICS_LIST', json)
    }).catch(() => {})
  },
  tactics_type_list ({ commit, state }, params) {
    return Api.typeList(params).then((json) => {
      commit('TACTICS_TYPE_LIST', json)
    }).catch(() => {})
  },
  tactics_info ({commit, state}, params) {
    return Api.details(params).then((res) => {
      let content = JSON.parse(res.data.content)
      content.id = res.data.id
      commit('TACTICS_INFO', content)
    }).catch(() => {})
  }
}

const mutations = {
  TACTICS_LIST (state, data) {
    state.tactics_list = data.data
  },
  SPORT (state, data) {
    state.sport = data
  },
  TACTICS_TYPE_LIST (state, res) {
    state.tactics_type_list = res.data
  },
  TACTICS_INFO (state, data = {}) {
    if (data.id && data.id !== state.tactics_info.id) {
      data.key = Math.random().toString(36).substr(2)
    }
    state.tactics_info = data
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
